﻿using AutoWinUI;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Windows.Automation;

namespace TestProject1
{
    
    
    /// <summary>
    ///This is a test class for InternetOptionsTest and is intended
    ///to contain all InternetOptionsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class InternetOptionsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for ClickCancelButton
        ///</summary>
        [TestMethod()]
        public void ClickCancelButtonTest()
        {
            Desktop.LaunchProgram("inetcpl.cpl");
            System.Threading.Thread.Sleep(5000);
            InternetOptions.ClickCancelButton();
            System.Threading.Thread.Sleep(5000);
            bool answer = InternetOptions.IsInternetOptionsLaunched();
            Assert.AreEqual(false, answer, "Internet Options - Click Cancel Button");
        
        }

        /// <summary>
        ///A test for ClickOKButton
        ///</summary>
        [TestMethod()]
        public void ClickOKButtonTest()
        {
            Desktop.LaunchProgram("inetcpl.cpl");
            System.Threading.Thread.Sleep(5000);
            InternetOptions.ClickOKButton();
            System.Threading.Thread.Sleep(5000);
            bool answer = InternetOptions.IsInternetOptionsLaunched();
            Assert.AreEqual(false, answer,"Internet Options - Click OK Button");           
          
        }

        /// <summary>
        ///A test for ClickApplyButton - also fro Use Blank button
        ///</summary>
        [TestMethod()]
        public void ClickApplyButtonTest()
        {
            bool answer = false;
            Desktop.LaunchProgram("inetcpl.cpl");
            System.Threading.Thread.Sleep(5000);
            if (InternetOptions.IsInternetOptionsLaunched())
            {
                InternetOptions.ClickUseBlank();
                System.Threading.Thread.Sleep(1000);
                
                // check apply button - 
                // click "Use Blank" button and check "Apply" property - IsKeyboardFocusable = true. if true, asnwer=true; otherwise, false;
                AutomationElement DT = AutomationElement.RootElement;
                AutomationElement internetoption = null;
                PropertyCondition p1 = new PropertyCondition(AutomationElement.AutomationIdProperty, "");
                PropertyCondition p2 = new PropertyCondition(AutomationElement.ClassNameProperty, "#32770");
                AndCondition and1 = new AndCondition(p1, p2);
                internetoption = DT.FindFirst(TreeScope.Children, and1);
                AutomationElement apply = null;
                PropertyCondition p3 = new PropertyCondition(AutomationElement.AutomationIdProperty, "12321");
                PropertyCondition p4 = new PropertyCondition(AutomationElement.ClassNameProperty, "Button");
                AndCondition and2 = new AndCondition(p3, p4);
                apply = internetoption.FindFirst(TreeScope.Descendants, and2);
                if (apply.Current.IsKeyboardFocusable)
                    answer = true;
                else
                    answer = false;

                // click cancel button
                InternetOptions.ClickCancelButton();                
                System.Threading.Thread.Sleep(1000);             
            }

            Assert.AreEqual(true, answer, "Internet Options - Click Apply Button");           

        }

      
        /// <summary>
        ///A test for ClickTabSetting
        ///</summary>
        [TestMethod()]
        public void ClickTabSettingTest()
        {
            bool answer = false ;

            Desktop.LaunchProgram("inetcpl.cpl");
            System.Threading.Thread.Sleep(5000);

            InternetOptions.ClickTabSetting();
            System.Threading.Thread.Sleep(5000);

            //check whether Tab Browsing window there
            AutomationElement tabbrowse = IsTabBrowsingLaunched();
            if (tabbrowse == null)
                answer = false;
            else
                answer = true;

            //click cancel button on Tab Browsing window
            InternetOptions.ClickCancelonTabBrowsing();

            //click Cancel button on Internet Options window
            InternetOptions.ClickCancelButton();

            Assert.AreEqual(true, answer, "Check whether Tab browsing is open and close properly.");
        }

        AutomationElement IsInternetOptionsLaunched()
        {
            AutomationElement Desktop = AutomationElement.RootElement;
            PropertyCondition inetcplAutomationID = new PropertyCondition(AutomationElement.AutomationIdProperty, "");
            PropertyCondition inetcplClassName = new PropertyCondition(AutomationElement.ClassNameProperty, "#32770");
            AndCondition and1 = new AndCondition(inetcplAutomationID, inetcplClassName);
            return Desktop.FindFirst(TreeScope.Children, and1);
            
        }

        AutomationElement IsTabBrowsingLaunched()
        {
            AutomationElement InternetOptions = IsInternetOptionsLaunched();
            PropertyCondition  AutomationID = new PropertyCondition(AutomationElement.AutomationIdProperty, "");
            PropertyCondition  ClassName = new PropertyCondition(AutomationElement.ClassNameProperty, "#32770");
            AndCondition and1 = new AndCondition(AutomationID, ClassName);
            return InternetOptions.FindFirst(TreeScope.Children, and1);

        }


    }
}
